/** 
 * File-Name:LinkDatabase.java
 *
 * Created on 2010-11-21 下午07:23:37
 * 
 * @author: Neo (neolimeng@gmail.com)
 * Software Engineering Institute, Peking University, China
 * 
 * Copyright (c) 2009, Peking University
 * 
 *
 */
package org.neocrawler.util;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;
import org.neocrawler.dao.WebContentDao;
import org.neocrawler.entity.WebContent;

/**
 * Description:
 * 
 * @author: Neo (neolimeng@gmail.com) Software Engineering Institute, Peking
 *          University, China
 * @version 1.0 2010-11-21 下午07:23:37
 */
public class LinkDatabase {
	private static Logger log = Logger.getLogger(LinkDatabase.class);
	// 已访问的 url 集合
	private static Set<WebContent> visitedWebContent = new HashSet<WebContent>();
	// 待访问的 url 集合
	private static Queue<WebContent> unVisitedWebContent = new Queue<WebContent>();

	private static WebContentDao webContentDao = new WebContentDao();

	public static Queue<WebContent> getUnVisitedWebContent() {
		return unVisitedWebContent;
	}

	public static void removeVisitedWebContent(WebContent webContent) {
		visitedWebContent.remove(webContent);
	}

	public static synchronized WebContent unVisitedWebContentDeQueue() {
		if (getUnvisitedWebContentNum() > 0) {
			return unVisitedWebContent.deQueue();
		} else {
			return null;
		}
	}

	private static int i = 0;
	private static int j = 0;
	private static int k = 0;

	// 保证每个 url 只被访问一次
	public static synchronized void addUnvisitedWebContent(WebContent webContent) {
		if (webContent != null && webContent.getUrl() != null
				&& !webContent.getUrl().trim().equals("")
				&& !visitedWebContent.contains(webContent)
				&& !unVisitedWebContent.contians(webContent)) {
			unVisitedWebContent.enQueue(webContent);
			webContentDao.saveOrUpdate(webContent);
			log.debug("unVisitedWebContent队列中添加新对象之后个数为："
					+ unVisitedWebContent.size());
		} else {
			log.debug(webContent != null ? webContent.getUrl() + "已经被访问过！略过~~"
					: "WebContent为空！！！");
		}
	}

	public static synchronized void addUnvisitedWebContents(
			List<WebContent> webContents) {
		if (webContents == null) {
			log.info("addUnvisitedWebContents中传入的webContents为null！");
			return;
		}
		List<WebContent> contentsToSave = new ArrayList<WebContent>();
		for (WebContent webContent : webContents) {
			if (webContent != null && webContent.getUrl() != null
					&& !webContent.getUrl().trim().equals("")
					&& !visitedWebContent.contains(webContent)
					&& !unVisitedWebContent.contians(webContent)) {
				unVisitedWebContent.enQueue(webContent);
				contentsToSave.add(webContent);
				log.debug("unVisitedWebContent队列中添加新对象之后个数为："
						+ unVisitedWebContent.size());
			} else {
				log.debug(webContent != null ? webContent.getUrl()
						+ "已经被访问过！略过~~" : "WebContent为空！！！");
			}
		}
		// Long startLong = System.currentTimeMillis();
		webContentDao.saveOrUpdate(contentsToSave);
		// System.err.println(System.currentTimeMillis() - startLong);
	}

	public static synchronized void addUnvisitedWebContentsWithoutSaveOrUpdate(
			List<WebContent> webContents) {

		if (webContents == null) {
			log.debug("addUnvisitedWebContents中传入的webContents为null！");
			return;
		}
		for (WebContent webContent : webContents) {
			if (webContent != null && webContent.getUrl() != null
					&& !webContent.getUrl().trim().equals("")
					&& !visitedWebContent.contains(webContent)
					&& !unVisitedWebContent.contians(webContent)) {
				unVisitedWebContent.enQueue(webContent);
				log.debug("unVisitedWebContent队列中添加新对象之后个数为："
						+ unVisitedWebContent.size());
			} else {
				log.debug(webContent != null ? webContent.getUrl()
						+ "已经被访问过！略过~~" : "WebContent为空！！！");
			}
		}
	}

	public static void addVisitedWebContent(WebContent webContent) {
		visitedWebContent.add(webContent);
	}

	public static synchronized void addVisitedWebContents(
			List<WebContent> webContents) {
		if (webContents == null) {
			log.debug("addVisitedWebContents中传入的webContents为null！");
			return;
		}
		for (WebContent webContent : webContents) {
			addVisitedWebContent(webContent);
			log.debug("visitedWebContent队列中添加新对象之后个数为："
					+ visitedWebContent.size());
		}
	}

	public static synchronized Integer getUnvisitedWebContentNum() {
		return unVisitedWebContent.size();
	}

	public static synchronized Integer getVisitedWebContentNum() {
		return visitedWebContent.size();
	}

	public static synchronized boolean unVisitedWebContentsEmpty() {
		return unVisitedWebContent.empty();
	}

	/**
	 * Description:
	 * 
	 * @param args
	 *            void
	 */
	public static void main(String[] args) {
		WebContent webContent = new WebContent();
		webContent.setUrl("http://wwww.com");

		WebContent webContent2 = new WebContent();
		webContent2.setUrl("http://wwww.com");
		webContent2.setStatus(WebContent.STATUS_EXCEPTION);

		LinkDatabase.addVisitedWebContent(webContent);
		System.out
				.println(LinkDatabase.visitedWebContent.contains(webContent2));
	}

}
